package com.example.administrator.testxingpiao.dao;

import android.content.Context;
import android.util.Log;

import com.example.administrator.testxingpiao.bean.CartBean;
import com.example.administrator.testxingpiao.utils.ORMLiteCartHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/9.
 */
public class CartDao {
    private ORMLiteCartHelper ormLiteHelper;
    private static Dao<CartBean, Integer> dao;
    private static CartDao mDao;

    private CartDao(Context context){

        this.ormLiteHelper = new ORMLiteCartHelper(context);

        try {
            dao = ormLiteHelper.getDao(CartBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //单例Dao
    private static CartDao cartDaoInstance(Context context){
        if(mDao == null){
            mDao = new CartDao(context);
        }
        return mDao;
    }

    //增
    public static int  add(CartBean cart, Context context){
        cartDaoInstance(context);
        int add = -1;
        try {
            add = dao.create(cart);
            log("add=" + add);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return add;
    }

    //删
    public static int  delete(Context context,CartBean cart){
        cartDaoInstance(context);
        int delete = -1;
        DeleteBuilder<CartBean, Integer> builder = dao.deleteBuilder();
        try {
            builder.where().eq("name",cart.getName());
            delete = builder.delete();
            log("delete" + delete);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return delete;
    }
    public static int deleteAll(Context context){
        cartDaoInstance(context);
        int delete = -1;
        DeleteBuilder<CartBean, Integer> builder = dao.deleteBuilder();
        try {
            delete = builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return delete;
    }
    //改
    public static int update(Context context,CartBean cart){
        cartDaoInstance(context);
        UpdateBuilder<CartBean, Integer> builder = dao.updateBuilder();
        int update = -1;
        try {
            builder.where().eq("name",cart.getName());
            builder.updateColumnValue("count",cart.getCount());
            update = builder.update();
            log("update = " + update);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return update;
    }
    //查全部
    public static List<CartBean> queryAll(Context context){

        List<CartBean> cartList= new ArrayList<>();
        cartDaoInstance(context);

        try {
            cartList= dao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cartList;
    }
    //查一个
    public static CartBean queryOne(Context context, CartBean cb){

        List<CartBean> cartList = new ArrayList<>();

        QueryBuilder<CartBean, Integer> builder = dao.queryBuilder();

        try {
            builder.where().eq("name",cb.getName());
            cartList = builder.query();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        if(cartList.size() > 0){
            return cartList.get(0);
        }else {
            return null;
        }
    }

    public static void log(String msg){
        Log.i("TAG",msg);
    }
}
